package com.housediyadmin.view.components
{
	/**
	 * 
	 * @author wenjun cai
	 * 2010/11/24
	 */	
	import com.housediyadmin.events.ShowDetailEve;
	import com.housediyadmin.events.brandEvents.AddBrandEvent;
	import com.housediyadmin.events.brandEvents.EditBrandEvent;
	import com.housediyadmin.model.BrandModel;
	import com.housediyadmin.vo.Brand;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.formatters.DateFormatter;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	public class EditBrandMediator extends Mediator
	{
		[Inject]
		public var view:EditBrandView;
		
		[Inject]
		public var brandModel:BrandModel;
		
		[Bindable]
		public var fileName:String;
		
		public var file:FileReference;
		public var request:URLRequest;
		private var oldBrand:Brand;
		private var newBrand:Brand;
		
		override public function onRegister():void
		{
			//request = new URLRequest("http://localhost/housediyadmin/BrandsUploadService.aspx");
			request = new URLRequest("http://10.60.1.120/HouseDiyAdmin/BrandsUploadService.aspx");
			file = new FileReference();
			file.addEventListener(Event.SELECT, selectFileHandler);
			file.addEventListener(ProgressEvent.PROGRESS, uploadProgressHandler);
			view.browseBtn.addEventListener(MouseEvent.CLICK, browseFileHandler);
			
			
			var dateFormat:DateFormatter=new DateFormatter();
			dateFormat.formatString="YYYY年MM月DD日";
			newBrand=new Brand();
			
			if(view.type=="ADD")
			{
				view.createDateLbl.text=dateFormat.format(new Date());
				view.editDateLbl.text=dateFormat.format(new Date());
			}
			else if(view.type=="EDIT")
			{
				oldBrand=brandModel.selectedBrand;
				view.createDateLbl.text=dateFormat.format(oldBrand.brandCreateDate);
				view.editDateLbl.text=dateFormat.format(new Date());
				view.nameTxt.text=oldBrand.brandName;
				view.descTxt.text=oldBrand.brandIntroduce;
				view.creatorTxt.text=oldBrand.brandCreator;
				view.editorTxt.text=oldBrand.brandEditor;
				view.logoPathTxt.text=oldBrand.brandPicSrc;
			}
			view.addEventListener(CloseEvent.CLOSE,close);
			view.okBtn.addEventListener(MouseEvent.CLICK,okClk);
			view.cancelBtn.addEventListener(MouseEvent.CLICK,close);
		}
		
		public function selectFileHandler(event:Event):void
		{
			//view.uploadBar.visible = true;
			file = FileReference(event.target);
			var variables:URLVariables = new URLVariables();
			variables.fileName  = "caddy" + "_" + new Date().time + file.type;
			fileName = "caddy" + "_" + new Date().time + file.type;	
			request.data = variables;
			file.upload(request);
			view.logoPathTxt.text = fileName;
			
		}			
		
		public function uploadProgressHandler(event:ProgressEvent):void
		{
			//view.uploadBar.setProgress(event.bytesLoaded/event.bytesTotal*100, 100) + "%";
			//view.uploadBar.label = "当前进度" + int(event.bytesLoaded/event.bytesTotal*100) + "%";
		}
		
		public function browseFileHandler(event:MouseEvent):void
		{
			file.browse(getTypes());
		}
		
		private function getTypes():Array
		{
			var allTypes:Array = new Array(getImageTypeFilter());
			return allTypes;
		}
		private function getImageTypeFilter():FileFilter
		{
			return new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
		}
		
		private function okClk(e:MouseEvent):void
		{
			if(view.type=="ADD")
			{
				newBrand.brandName=view.nameTxt.text;
				newBrand.brandIntroduce=view.descTxt.text;
				newBrand.brandPicSrc=view.logoPathTxt.text;
				newBrand.brandCreator=view.creatorTxt.text;
				newBrand.brandEditor=view.editorTxt.text;
				newBrand.brandCreateDate=new Date();
				newBrand.brandEditDate=new Date();
				dispatch(new AddBrandEvent(AddBrandEvent.ADD_BRAND,newBrand));
				Alert.yesLabel="确定";
				Alert.show("添加成功！","",1,view,okAlert);
			}
			else if(view.type=="EDIT")
			{
				newBrand.brandID=oldBrand.brandID;
				newBrand.brandCreateDate=oldBrand.brandCreateDate;
				newBrand.brandCreator=oldBrand.brandCreator;
				newBrand.brandEditDate=new Date();
				newBrand.brandEditor=view.editorTxt.text;
				newBrand.brandIntroduce=view.descTxt.text;
				newBrand.brandName=view.nameTxt.text;
				newBrand.brandPicSrc=view.logoPathTxt.text;
				dispatch(new EditBrandEvent(EditBrandEvent.EDIT_BRAND,newBrand));
				
				Alert.yesLabel="确定";
				Alert.show("修改成功！","",1,view,okAlert);
			}
		}
		
		private function okAlert(e:CloseEvent):void
		{
			dispatch(new ShowDetailEve(ShowDetailEve.BrandDetail,newBrand));
			close(e);

		}
		
		private function close(e:Event):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediator( this );
		}
	}
}